IF  EXISTS ( SELECT * 
             FROM   master.sys.server_triggers 
             WHERE  parent_class_desc = 'SERVER' 
             AND    name = N'LogAllDDL' )
BEGIN
     IF EXISTS ( SELECT 'X' 
                 FROM   sys.server_triggers 
                 WHERE  parent_class_desc = 'SERVER' 
                 AND    name              = 'LogAllDDL' 
                 AND    is_disabled       = 0 )
     BEGIN
          EXEC( 'DISABLE TRIGGER [LogAllDDL] ON ALL SERVER' )
     END

     EXEC( 'DROP TRIGGER [LogAllDDL] ON ALL SERVER' )
END
GO

CREATE TRIGGER [LogAllDDL]
ON ALL SERVER 
WITH EXECUTE AS 'AuditCredLogin', ENCRYPTION 
FOR DDL_EVENTS
AS
BEGIN
     SET NOCOUNT ON
     SET ANSI_PADDING ON

     DECLARE @data xml
     SET @data = EVENTDATA()

     EXEC DBAObjects.dbo.InsDDLActivity @EventData = @data
END
GO

--Enable the trigger.
IF EXISTS ( SELECT 'X' 
            FROM   sys.server_triggers 
            WHERE  parent_class_desc = 'SERVER' 
            AND    name              = 'LogAllDDL' 
            AND    is_disabled       = 1 )
BEGIN
     EXEC ( 'ENABLE TRIGGER [LogAllDDL] ON ALL SERVER' )
END
GO
